package com.cctv.portal.sms.adapter.ws;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

public final class RespParser {
	
	@SuppressWarnings("unchecked")
	public static RespBody parseXml(String result) throws DocumentException {
		RespBody respBody = new RespBody();
		Document doc = DocumentHelper.parseText(result);
		Element rootElt = doc.getRootElement(); // 获取根节点
		respBody.setCode(rootElt.element("code").getTextTrim());
		List<Message> messageList = new ArrayList<Message>();
		List<Element> messages = rootElt.elements("message");
		for(Element mele : messages){
			Message message = new Message();
			message.setMsgid(mele.elementTextTrim("msgid"));
			message.setDesmobile(mele.elementTextTrim("desmobile"));
			messageList.add(message);
		}
		respBody.setMessage(messageList);
		return respBody;
	}
	
	public static void main(String[] args) {
		String result = "<?xml version=\"1.0\" encoding=\"gbk\" ?><response><code>03</code><message><desmobile>13810180366</desmobile><msgid>D6875314052714500800</msgid></message></response>";
		try {
			System.out.println(parseXml(result));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
}
